<?php

namespace app\admin\controller;

use app\common\controller\Backend;



/**
 * 产品留言
 *
 * @icon fa fa-circle-o
 */
class UserEvaluation extends Backend
{
    
    /**
     * UserEvaluation模型对象
     * @var \app\admin\model\UserEvaluation
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserEvaluation;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    public function index(){
        $userEvaluation=db("user_evaluation");
        $product=db("product");
        $listData=$userEvaluation->where("mstatus",1)->select();

        foreach($listData as $k=>$v){
            $pname=$product->find($v['pid']);
            $listData[$k]['p_name']=$pname['s_name'];
        }

       // dump($listData);
        $evaluation_score=getArr("evaluation-score");
        $this->assign([
            'eList'=>$listData,
            'evaluation_score'=> $evaluation_score,
        ]);
        return $this->view->fetch();

    }

    public function changeStatus(){
        $userEvaluation=db("user_evaluation");
        if(request()->isAjax()){
            $id=input("post.id");
            $res=$userEvaluation->find($id);
            if($res['mstatus']==1){
                $update['mstatus']=0;
            }else{
                $update['mstatus']=1;
            }
            $result=$userEvaluation->where("id",$id)->update($update);
            if($result){
                ajaxMsg("状态修改成功",1);
            }else{
                ajaxMsg("状态修改失败",0);
            }
        }
    }

    public function perform_del(){
        $userEvaluation=db("user_evaluation");
        $articleImg=db("article_img");
        if(request()->isAjax()){
            $id=input("post.id");
            $articleImgList=$articleImg->where(["info_id"=>$id,"info_type"=>4])->select();
            foreach($articleImgList as $k=>$v){
                $path=ROOT_PATH . 'public'.$v['imgadr'];
                @unlink($path);
                $res= db("article_img")->delete($v['id']);
                if(!$res){
                    ajaxMsg("评论图片删除失败",0);
                }
            }
            $result=$userEvaluation->delete($id);
            if($result){
                ajaxMsg("评论删除成功",1);
            }else{
                ajaxMsg("评论删除失败",0);
            }
        }
    }


    public function evaluation_edit(){

        $id=input("get.id");
        $article_img=db("article_img");
        $userEvaluation=db("user_evaluation");
        $res=$userEvaluation->find($id);
        $evaluation_score=getArr("evaluation-score");
        $imgList=db("article_img")->where(["info_id"=>$id,"info_type"=>4])->select();
        $resArr=array_flip($evaluation_score);
        //dump($resArr);
        if(request()->isPost()){
            $data=input("post.");
            // dump($data);
            $count=count($data);
            $imgCount=$count-9;
            $img_temp=$data['img_temp'];
            if(strpos($img_temp,"|")){
                $img_temp_arr=explode("|",$img_temp);
                $img_temp_arr_count=count($img_temp_arr);
                for($i=0;$i<$img_temp_arr_count;$i++){
                    if($img_temp_arr[$i]>0){
                        $res_article_img=$article_img->find($img_temp_arr[$i]);
                        if($res_article_img['imgadr']!=''){
                            $img_url1=ROOT_PATH . 'public'.$res_article_img['imgadr'];
                            @unlink($img_url1);
                        }
                        $article_img->delete($res_article_img['id']);
                    }
                }
            }

            if($imgCount>0){
                for($i=0;$i<$imgCount;$i++){
                    $imgURL=base64_image_content($data["img".$i]);
                    $img_url['imgadr']=$imgURL['url'];
                    $img_url['info_id']=$data['id'];
                    $img_url['info_type']=4;
                    $article=$article_img->insertGetId($img_url);
                }
            }

            $update['pnum']=$data['pnum'];
            $update['guide_num']=$data['guide_num'];
            $update['trip_num']=$data['trip_num'];
            $update['food_num']=$data['food_num'];
            $update['service_num']=$data['service_num'];
            $update['mstatus']=$data['mstatus'];
            $update['pmsg']=$data['pmsg'];
            $update['id']=$data['id'];

            $updateRes=$userEvaluation->update($update);

            if($updateRes!==false){
                $this->success("修改成功","/admin/user_evaluation/index");
            }else{
                $this->error("修改失败");
            }
        }

        $this->assign([
            "res"=>$res,
            "evaluation_score"=>$evaluation_score,
            "resArr"=>$resArr,
            "imgList"=>$imgList,
        ]);
        return $this->view->fetch();
}










    








    

}
